package com.subaiqiao.yunshiyuan.entrance.ysy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subaiqiao.yunshiyuan.basis.controller.BasisController;
import com.subaiqiao.yunshiyuan.basis.utils.Result;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.Article;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹朝禹
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/ysy/article")
@Api(tags = "消息信息")
public class ArticleController extends BasisController<Article, ArticleService> {

    @Autowired
    private ArticleService articleService;

    @Override
    public ArticleService getService() {
        return articleService;
    }

    @ApiOperation("查询所有消息信息")
    @GetMapping("/FindArticle.do")
    public Result FindArticle(
            @RequestParam @ApiParam("页码") Integer pageNum,
            @RequestParam @ApiParam("数量") Integer pageSize,
            @RequestParam(defaultValue = "") @ApiParam("消息标题") String articleTitle,
            @RequestParam(defaultValue = "") @ApiParam("消息类型") Integer articleCategoryId,
            @ApiParam("开始时间") Date startTime,
            @ApiParam("结束时间") Date endTime
    ){
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.like("article_title", articleTitle)
        .orderByDesc("create_datetime");
        if(articleCategoryId!=null){
            wrapper.eq("article_category", articleCategoryId);
        }
        if(startTime!=null){
            System.out.println("开始时间："+startTime);
            wrapper.ge("create_datetime", startTime);
        }
        if(endTime!=null){
            System.out.println("结束时间："+endTime);
            wrapper.le("create_datetime", endTime);
        }
        Page<Article> page = new Page<>(pageNum,pageSize);
        return Result.success(articleService.page(page, wrapper));
    }

    @ApiOperation("添加消息")
    @PostMapping("/InsertArticle.do")
    public Result InsertArticle(@ApiParam("消息对象") Article article){
        if(article.getArticleId()!=null){
            return Result.success(articleService.updateById(article));
        }else{
            article.setStatus(0);
            article.setArticleCheckQuantity(0);
            return Result.success(articleService.save(article));
        }
    }

    @ApiOperation("查询消息详情")
    @GetMapping("/FindArticleDetails.do")
    public Result FindArticleDetails(@RequestParam(defaultValue = "") @ApiParam("消息编号") Integer articleId){
        Article article = articleService.getById(articleId);
        article.setArticleCheckQuantity(article.getArticleCheckQuantity()+1);
        articleService.updateById(article);
        return Result.success(articleService.FindById(article.getArticleId()));
    }

    @ApiOperation("删除一条消息")
    @DeleteMapping("/DeleteArticle.do")
    public Result DeleteArticle(@RequestParam @ApiParam("商品编号") Integer articleId){
        return Result.success(articleService.removeById(articleId));
    }

    @ApiOperation("查询消息总数量")
    @GetMapping("/FindArticleCount.do")
    public Result FindArticleCount(){
        return Result.success(articleService.FindArticleCount());
    }

    @ApiOperation("查询禁用消息总数量")
    @GetMapping("/FindArticleDisableCount.do")
    public Result FindArticleDisableCount(){
        return Result.success(articleService.FindArticleDisableCount());
    }

    @ApiOperation("查询昨日发布消息总数")
    @GetMapping("/FindArticleCountYesterdayRegister.do")
    public Result FindArticleCountYesterdayRegister(){
        return Result.success(articleService.FindArticleCountYesterdayRegister());
    }


}
